define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods_cards/index' + location.search,
                    add_url: 'goods/goods_cards/add',
                    edit_url: 'goods/goods_cards/edit',
                    del_url: 'goods/goods_cards/del',
                    multi_url: 'goods/goods_cards/multi',
                    import_url: 'goods/goods_cards/import',
                    table: 'goods_cards',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                 maintainSelected: true,
                exportDataType: 'selected',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                       // {field: 'goods_id', title: __('Goods_id')},
                      //  {field: 'business_id', title: __('Business_id')},
                        {field: 'goods.goods_title', title: __('Goods.goods_title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'business.business_name', title: __('Business.business_name'), operate: 'LIKE'},
                   //     {field: 'goods_cards_no', title: __('Goods_cards_no'), operate: 'LIKE'},
                        {field: 'goods_card_key', title: __('Goods_card_key'), operate: 'LIKE'},
                       // {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('激活用户昵称'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('激活用户手机号'), operate: 'LIKE'},
                        {field: 'use_status', title: __('Use_status'), searchList: {"0":__('Use_status 0'),"1":__('Use_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                pageList:[25,50,100,200,'all'],


            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on('click','#addcard',function (event) {
                Fast.api.open('goods/goods_cards/addcard', '批量添加卡密', {
                    callback: function (data) {
                        $(".btn-refresh").trigger("click");
                    },
                });
            });
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;

                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("goods/goods_cards/export") + "' method='post' id='cc' target='_blank'><input type='hidden' name='ids' id='kk' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    },
                    yes: function (index, layero) {


                        if (ids.length > 0) {
                            var form = document.getElementById('cc');
                            var custmoreInput = document.getElementById('kk');
                            custmoreInput.setAttribute("value", ids.join(","));
                            // console.log(ids)
                            var options = table.bootstrapTable('getOptions');
                            var search = options.queryParams({});
                            $("input[name=search]", layero).val(options.searchText);
                            $("input[name=filter]", layero).val(search.filter);
                            $("input[name=op]", layero).val(search.op);
                            form.submit(ids.join(","), layero);
                            return true;
                        }
                        return false;
                    },
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        if (ids.length > 0) {
                            var form = document.getElementById('cc');
                            var custmoreInput = document.getElementById('kk');
                            custmoreInput.setAttribute("value", ids.join(","));
                            // console.log(ids);
                            // console.log( custmoreInput.getAttribute("value"));
                            // console.log(form);
                            //<input type="hidden" name="ids" id="kk" value="" />
                            var options = table.bootstrapTable('getOptions');
                            var search = options.queryParams({});
                            $("input[name=search]", layero).val(options.searchText);
                            $("input[name=filter]", layero).val(search.filter);
                            $("input[name=op]", layero).val(search.op);
                            form.submit(ids.join(","), layero);
                            return true;
                        }
                        return false;
                    },
                    btn3: function (index, layero) {
                        var form = document.getElementById('cc');
                        var custmoreInput = document.getElementById('kk');
                        custmoreInput.setAttribute("value", "all");
                        // console.log(all);
                        var options = table.bootstrapTable('getOptions');
                        var search = options.queryParams({});
                        $("input[name=search]", layero).val(options.searchText);
                        $("input[name=filter]", layero).val(search.filter);
                        $("input[name=op]", layero).val(search.op);
                        form.submit("all", layero);
                        return true;
                    }
                })
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        addcard:function () {

            Form.api.bindevent("form[role=form]", function(data, ret){
                Fast.api.close(data);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
